package study_HttpClient;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;

public class HttpClient的Get与Post方法 {
    public static void main(String[] args) {
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2.创建HttpGet对象，设置URL访问地址
        HttpGet httpGet = new HttpGet("http://www.baidu.com");
        //HttpPost httpPost = new HttpPost("http://www.baidu.com");

        //HttpHost proxy = new HttpHost("192.168.1.101",8080,"http");

        //配置请求信息
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(1000)  //创建连接的最长时间，单位毫秒
                .setConnectionRequestTimeout(500)  //设置获取连接的最长时间，单位是毫秒
                .setSocketTimeout(10*1000)   //设置数据传输的最长时间
                //可以继续设置代理等配置信息
                //.setProxy(proxy)
                .build();

        //给请求设置请求信息
        httpGet.setConfig(config);

        CloseableHttpResponse response = null;

        //3.使用HttpClient发起请求，获取response
        try {
            response = httpClient.execute(httpGet);

            //4.解析响应
            if (response.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(response.getEntity(),"utf8");
                System.out.println(content.length());
            }

        }catch (IOException e){
            e.printStackTrace();
        }finally {
            //5.关闭response
            try {
                response.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
            //6.关闭httpClient
            try {
                httpClient.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
